﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Events;
using JBeamLibrary.VehicleParts.Elements;
using System.Collections.ObjectModel;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Collections
{
    public class SlotCollection : KeyedCollection<string, Slot>, IVehicleGroupCollection<Slot>
    {
        #region Fields

        public event ElementPropertyChangedEventHandler ElementPropertyChanged;

        #endregion

        #region CTORs

        /// <summary>Initializes a new empty slot collection</summary>
        public SlotCollection() { }

        /// <summary>Initializes a new slot collection filled with the given slots</summary>
        /// <param name="slots"></param>
        public SlotCollection(IEnumerable<Slot> slots)
        {
            AddRange(slots);
        }

        #endregion

        #region Methods

        public new void Add(Slot slot)
        {
            base.Add(slot);

            slot.PropertyChanged += (s, e) =>
            {
                if (ElementPropertyChanged != null)
                    ElementPropertyChanged(s, e);
            };
        }

        public void Add(string typeName, string description, VehiclePart parentPart, string defaultPartName = "")
        {
            Add(new Slot(typeName, description, parentPart) { DefaultPartName = defaultPartName });
        }

        public void AddRange(IEnumerable<Slot> slots)
        {
            foreach (var slot in slots)
                Add(slot);
        }

        public void UpdateAdd(string typeName, string description, VehiclePart parentPart, string defaultPartName = null)
        {
            UpdateAdd(new Slot(typeName, description, parentPart) { DefaultPartName = defaultPartName });
        }

        public void UpdateAdd(Slot slot)
        {
            if (Contains(slot.TypeName))
                Remove(slot.TypeName);
            Add(slot);
        }

        public void UpdateAddRange(IEnumerable<Slot> slots)
        {
            foreach (var slot in slots)
                UpdateAdd(slot);
        }

        public new void Remove(Slot element)
        {
            base.Remove(element);
        }

        #endregion

        #region KeyedCollection

        protected override string GetKeyForItem(Slot item)
        {
            return item.TypeName;
        }

        #endregion
    }
}
